#import <UIKit/UIKit.h>
#import "MRedBagHead.h"
#import "HongBaoQueue.h"

%hook WCRedEnvelopesLogicMgr
- (void)OnWCToHongbaoCommonResponse:(HongBaoRes*)arg1 Request:(HongBaoReq*)arg2{
 
    NSLog(@"SKBuiltinBuffer_t：%@",[[NSString alloc] initWithData:arg1.retText.buffer encoding:NSUTF8StringEncoding]);
   NSError *err;
   NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:arg1.retText.buffer options:NSJSONReadingMutableContainers error:&err];
    //hbType==1群红包 ==0个人红包  hbStatus==2 没领红包可领
    if(dic!=nil&&[[dic objectForKey:@"hbType"] intValue]==1&&[[dic objectForKey:@"hbStatus"] intValue]==2){
  
        NSMutableDictionary* mudic= [[HongBaoQueue shareQueue] getFromQueue];
        if(mudic!=nil){
            [mudic setObject:[dic objectForKey:@"timingIdentifier"] forKey:@"timingIdentifier"];
            
            //手动拆红包
            float time=0;
            if([GYUserDefault valueForKey:GYTIMEKEY]&&[[GYUserDefault valueForKey:GYTIMEKEY] length]>0){
                time=[[GYUserDefault valueForKey:GYTIMEKEY] floatValue];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
                WCRedEnvelopesLogicMgr* logicmgr= [[%c(MMContext) currentContext] getService:[%c(WCRedEnvelopesLogicMgr) class]];
                [logicmgr OpenRedEnvelopesRequest:mudic];
            });
            
           
        }
    }
    
    %orig;
}

%end

%hook CMessageMgr
- (void)onNewSyncAddMessage:(CMessageWrap*)msgwrap {
    NSLog(@"收到消息：%@",msgwrap);
    if(msgwrap.m_uiMessageType==49&&[msgwrap.m_oWCPayInfoItem.m_c2cNativeUrl containsString:@"wxpay://c2cbizmessagehandler/hongbao/receivehongbao?"]&&[GYUserDefault boolForKey:GYSWITCHKEY]){
        NSLog(@"收到红包消息并且打开开关%@", [GYUserDefault valueForKey:GYTIMEKEY]);
        if([GYUserDefault valueForKey:GYTIMEKEY]){
            NSLog(@"存在时间%@",[GYUserDefault valueForKey:GYTIMEKEY]);
        }

 
        
        NSString* m_c2cNativeUrl=msgwrap.m_oWCPayInfoItem.m_c2cNativeUrl;
        long length=[@"wxpay://c2cbizmessagehandler/hongbao/receivehongbao?" length];
        NSString* sub_nativeUrl= [m_c2cNativeUrl substringFromIndex:length];

        NSMutableDictionary* mudic15=[%c(WCBizUtil) dictionaryWithDecodedComponets:sub_nativeUrl separator:@"&"];
        NSMutableDictionary* mudic=[NSMutableDictionary dictionary];
        [mudic setObject:@"1" forKey:@"msgType"];
        [mudic setObject:[mudic15 objectForKey:@"sendid"] forKey:@"sendId"];
        [mudic setObject:[mudic15 objectForKey:@"channelid"] forKey:@"channelId"];

        CContactMgr* conmgr=[[%c(MMContext) currentContext] getService:[%c(CContactMgr) class]];

         CContact* contact=[conmgr getSelfContact];

         [mudic setObject:[contact getContactDisplayName] forKey:@"nickName"];
         [mudic setObject:contact.m_nsHeadImgUrl forKey:@"headImg"];
    //   [mudic setObject:arg1 forKey:@"left_button_continue"];

        if (msgwrap)
        {
            WCPayInfoItem* infoitem=msgwrap.m_oWCPayInfoItem;
            NSString* c2nativeurl=infoitem.m_c2cNativeUrl;
            [mudic setObject:c2nativeurl forKey:@"nativeUrl"];
        }
        [mudic setObject:msgwrap.m_nsFromUsr forKey:@"sessionUserName"];
        NSLog(@"已构造的参数：%@",mudic);
        //保存进队列
        [[HongBaoQueue shareQueue] AddToQueue:mudic];
        
       //模拟手动触发打开红包页面的动作 获取timingIdentifier
       NSMutableDictionary* param=[NSMutableDictionary dictionary];
       [param setObject:@"0" forKey:@"agreeDuty"];
        [param setObject:mudic[@"channelId"] forKey:@"channelId"];
        [param setObject:@"1" forKey:@"inWay"];
        [param setObject:mudic[@"msgType"] forKey:@"msgType"];
        [param setObject:mudic[@"nativeUrl"] forKey:@"nativeUrl"];
        [param setObject:mudic[@"sendId"] forKey:@"sendId"];
        
        WCRedEnvelopesLogicMgr* logicmgr= [[%c(MMContext) currentContext] getService:[%c(WCRedEnvelopesLogicMgr) class]];
        [logicmgr ReceiverQueryRedEnvelopesRequest:param];

    }else{
        %orig;
    }

    %orig;
    
  
}
//- (void)AsyncOnPreAddMsg:(id)arg1 MsgWrap:(id)arg2 { %log; %orig; }
//- (void)AsyncOnAddMsg:(id)arg1 MsgWrap:(id)arg2 { %log; %orig; }
//- (void)onNewSyncAddMsgSessionArray:(id)arg1 withUsers:(id)arg2 { %log; %orig; }
//- (void)AsyncOnAddMsgListForSession:(id)arg1 NotifyUsrName:(id)arg2 { %log; %orig; }
//- (void)onNewSyncShowPush:(id)arg1 isAlwaysShow:(_Bool)arg2 { %log; %orig; }
//- (void)AsyncOnPushMsg:(id)arg1 isAlwaysShow:(_Bool)arg2 { %log; %orig; }
%end

//拆红包button
//%hook WCRedEnvelopesReceiveHomeView
//- (void)OnOpenRedEnvelopes{
//
//    NSDictionary * dic= MSHookIvar<NSDictionary*>(self, "m_dicBaseInfo");
//    NSLog(@"open参数：%@",dic);
//    id mydelegete=MSHookIvar<id>(self,"m_delegate");
//    NSLog(@"%@",[mydelegete class]);
//    %orig;
//    //[WCRedEnvelopesReceiveControlLogic   WCRedEnvelopesReceiveHomeViewOpenRedEnvelopes]
//}
//%end
//
//%hook WCRedEnvelopesReceiveControlLogic
//
//- (void)WCRedEnvelopesReceiveHomeViewOpenRedEnvelopes:(id)arg1{
//    [self reportReceiveHomeViewEmoticon];
//
//    WCRedEnvelopesControlData *m_data=MSHookIvar<WCRedEnvelopesControlData*>(self,"m_data");
//
//    CMessageWrap* msgwrap=m_data.m_oSelectedMessageWrap;
//    NSLog(@"msgwrap====%@",msgwrap);
    
    

//    WCPayInfoItem *m_oWCPayInfoItem=msgwrap.m_oWCPayInfoItem;
//    NSString* m_c2cNativeUrl=m_oWCPayInfoItem.m_c2cNativeUrl;
//    long length=[@"wxpay://c2cbizmessagehandler/hongbao/receivehongbao?" length];
//    NSString* sub_nativeUrl= [m_c2cNativeUrl substringFromIndex:length];
//
//    NSMutableDictionary* mudic15=[%c(WCBizUtil) dictionaryWithDecodedComponets:sub_nativeUrl separator:@"&"];
//
//
//    NSMutableDictionary* mudic=[NSMutableDictionary dictionary];
//    [mudic setObject:@"1" forKey:@"msgType"];
//    [mudic setObject:[mudic15 objectForKey:@"sendid"] forKey:@"sendId"];
//    [mudic setObject:[mudic15 objectForKey:@"channelid"] forKey:@"channelId"];
//
//
//    CContactMgr* conmgr=[[%c(MMContext) currentContext] getService:[%c(CContactMgr) class]];
//
//
//     CContact* contact=[conmgr getSelfContact];
//
//     [mudic setObject:[contact getContactDisplayName] forKey:@"nickName"];
//     [mudic setObject:contact.m_nsHeadImgUrl forKey:@"headImg"];
////   [mudic setObject:arg1 forKey:@"left_button_continue"];
//
//    if (msgwrap)
//    {
//        WCPayInfoItem* infoitem=msgwrap.m_oWCPayInfoItem;
//        NSString* c2nativeurl=infoitem.m_c2cNativeUrl;
//        [mudic setObject:c2nativeurl forKey:@"nativeUrl"];
//    }
//
//    MMMsgLogicManager* logmanager=[[%c(MMContext) currentContext] getService:[%c(MMMsgLogicManager) class]];
//
//    WeixinContentLogicController* LogicC=  [logmanager GetCurrentLogicController];
//    if (LogicC)
//      {
//
//        CBaseContact* bc=LogicC.m_contact;
//        if (bc)
//        {
//          if (bc.m_nsUsrName)
//          {
//              [mudic setObject:LogicC.m_contact.m_nsUsrName forKey:@"sessionUserName"];
//          }
//        }
//      }
//    NSDictionary* baseinfo= m_data.m_structDicRedEnvelopesBaseInfo;
//    NSString* timing=[baseinfo stringForKey:@"timingIdentifier"];
//    if ([timing length]){
//        [mudic setObject:timing forKey:@"timingIdentifier"];
//    }
//
//    WCPayLogicMgr* paylog= [[%c(MMContext) currentContext] getService:[%c(WCPayLogicMgr) class]];
//    [paylog setRealnameReportScene:1003];
//
//    __weak WCRedEnvelopesReceiveControlLogic* selfweak=self;
//
//     WCPayLogicMgr* logmgr=[[%c(MMContext) currentContext] getService:[%c(WCPayLogicMgr) class]];
//
//     [logmgr checkHongbaoOpenLicense:[baseinfo objectForKeyedSubscript:@"agree_duty"] acceptCallback:^(){
//         [selfweak OpenRedEnvelopesRequest:mudic];
//     } denyCallback:^(){
//
//     }];


    //NSLog(@"打印=====%@",mm);
//}

//- (void)OpenRedEnvelopesRequest:(NSMutableDictionary*)mudic{
//    WCRedEnvelopesLogicMgr* logicmgr= [[%c(MMContext) currentContext] getService:[%c(WCRedEnvelopesLogicMgr) class]];
//
//    if([self isOpenIMHongBao]){
//        [mudic setObject:@"0" forKey:@"union_source"];
//        [logicmgr OpenOpenIMRedEnvelopesRequest:mudic];
//        NSLog(@"走了11111");
//    }else{
//        [logicmgr OpenRedEnvelopesRequest:mudic];
//        NSLog(@"走了222222");
//    }
//
//   // int logstatus= MSHookIvar<int>(self,"m_uiLogicStatus");
//   // logstatus=4;
//
//}

//%end
